package test2.demo1;

import java.util.ArrayList;
import java.util.List;

public class test {
    public static void main(String[] args) {
        //定义100个囚犯 囚犯的编号为1-200的随机数 位置为1-100
        List<Prisoner> list = new ArrayList<>();
        for (int i = 1; i <= 100; i++) {
            Prisoner p = new Prisoner(i, (int) (Math.random() * 100 + 1));
            //囚犯编号不能重复
            for (Prisoner p1 : list) {
                if (p.getNumber() == p1.getNumber()) {
                    i--;
                }
            }
            list.add(p);
        }
        System.out.println(list);
       while(true)
       {
           List<Prisoner> list2=new ArrayList<>();
           for(int i=1;i<list.size();i+=2)
           {
               list2.add(list.get(i));
           }
           if(list2.size()==1)
           {
               System.out.println(list2);
               break;
           }
           list=list2;
       }
    }
}
//定义一个囚犯类
class Prisoner{
    private int location;
    private int number;
    public Prisoner(int number,int location){
        this.number = number;
        this.location = location;
    }
    public Prisoner(){}

    public int getNumber() {
        return number;
    }


    public void setNumber(int number) {
        this.number = number;
    }

    public int getLocation() {
        return location;
    }

    public void setLocation(int location) {
        this.location = location;
    }

    @Override
    public String toString() {
        return "Prisoner{" +
                "location=" + location +
                ", number=" + number +
                '}';
    }
}

